package com.xuetang9.cloudhouse.common.service.impl;

import com.xuetang9.cloudhouse.common.service.BasicService;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 老九.大师兄
 * @version 1.0
 * @desc
 * @date 2020/2/29-11:09
 * @copyright &copy; xuetang9
 * @address 成都市西部国际金融中心2栋2201
 */
public class BasicServiceImpl<T,MT extends Mapper<T>> implements BasicService<T,MT> {

    @Autowired
    protected MT mapper;

    @Override
    public List<T> queryAll() {
        return mapper.selectAll();
    }

    @Override
    public List<T> queryByKey(T t) {
        return mapper.select(t);
    }

    @Override
    public boolean save(T data) {
        return mapper.insertSelective(data) > 0;
    }

    @Override
    public List<Integer> batchSave(List<T> datas) {
        List<Integer> results = new ArrayList<>(datas.size());
        datas.forEach(mapper::insertSelective);
        return results;
    }

    @Override
    public boolean modify(T data) {
        return mapper.updateByPrimaryKeySelective(data)>0;
    }

    @Override
    public boolean remove(Long key) {
        return mapper.deleteByPrimaryKey(key)>0;
    }

    @Override
    public int deleteByIds(Class<T> clazz, List<Object> ids) {
        Example example = new Example(clazz);
        example.createCriteria().andIn("id", ids);
        return this.mapper.deleteByExample(example);
    }
}
